package com.example.day9_sqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/***
 需求：网络请求数据--》展示在listview中
 1。将请求到的数据存储到数据库中
 2。从数据库中读取数据并展示


 */
public class Main2Activity extends AppCompatActivity {
    private MyHelper myHelper;
    private SQLiteDatabase sqLiteDatabase;
    private ListView listView;
    private MyAdapter myAdapter;


    private List<MusicBean.ResultBean.SongsBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        listView = findViewById(R.id.lv);
        myHelper = new MyHelper(this);
        sqLiteDatabase = myHelper.getWritableDatabase();
        //读取sp数据 ---》if else

      //  new MyTask().execute("http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s=邓紫棋&type=1&offset=40&total=true&limit=20");

        read();

    }

    private void read() {
        Cursor cursor = sqLiteDatabase.rawQuery("select * from song", new String[]{});
        if(cursor != null){
            while (cursor.moveToNext()){
                int id = cursor.getInt(0);
                String name = cursor.getString(1);
                String url = cursor.getString(2);
                MusicBean.ResultBean.SongsBean songsBean = new MusicBean.ResultBean.SongsBean();
                songsBean.setId(id);
                songsBean.setName(name);
                MusicBean.ResultBean.SongsBean.ArtistsBean artistsBean = new MusicBean.ResultBean.SongsBean.ArtistsBean();
                artistsBean.setImg1v1Url(url);
                ArrayList<MusicBean.ResultBean.SongsBean.ArtistsBean> artistsBeans = new ArrayList<>();
                artistsBeans.add(artistsBean);

                songsBean.setArtists(artistsBeans);


               // songsBean.set
                list.add(songsBean);
            }

            myAdapter = new MyAdapter(this,list);
            listView.setAdapter(myAdapter);
        }

    }

    class MyTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            StringBuffer stringBuffer = null;
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.connect();
                InputStream inputStream = httpURLConnection.getInputStream();
                byte[] bytes = new byte[1024];
                int len =0;
                stringBuffer = new StringBuffer();
                while ((len = inputStream.read(bytes))!=-1){
                    String s = new String(bytes, 0, len);
                    stringBuffer.append(s);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return stringBuffer.toString();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            MusicBean musicBean = gson.fromJson(s, MusicBean.class);
            List<MusicBean.ResultBean.SongsBean> songs = musicBean.getResult().getSongs();
            //存储到数据库
            for (MusicBean.ResultBean.SongsBean song : songs) {
                int id = song.getId();
                String name = song.getName();
                String img1v1Url = song.getArtists().get(0).getImg1v1Url();
                sqLiteDatabase.execSQL("insert into song values(?,?,?)",new String[]{id+"",name,img1v1Url});
            }
            //使用sp写入标记，记录已经网络请求


        }
    }



}
